package com.winz.cultureapp.filter.art;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.PopupWindow;

import com.winz.cultureapp.R;
import com.winz.cultureapp.adapter.ArtTypeAdapter;
import com.winz.cultureapp.entity.ArtTypeBean;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * 艺术鉴赏--类型选择
 */
public class ArtPopWindow extends PopupWindow implements AdapterView.OnItemClickListener {

    private View window;
    private Context context;
    private int position = -1;

    private ArtTypeAdapter mAdapter;

    private List<ArtTypeBean.DataBean.ListBean> mDatas = new ArrayList<>();

    private OnFinishListener listener;


    public ArtPopWindow(Context context){
        super(context);
        this.context = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        window = inflater.inflate(R.layout.pop_activity_location, null);


        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(false);

        //设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle();
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //设置SelectPicPopupWindow的View
        this.setContentView(window);
        window.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        initviews(window);
        loadDatas();
    }



    private void initviews(View window) {
        GridView gv_data = window.findViewById(R.id.gv_location);
        window.findViewById(R.id.btn_reset).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mAdapter.cleanAllState();
            }
        });
        window.findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null){
                    String data = mAdapter.getLastSeletPosition() == -1?"全部,0":mDatas.get(mAdapter.getLastSeletPosition()).categoryName + "," +mDatas.get(mAdapter.getLastSeletPosition()).artCategoryId;
                    // 记录选择位置
                    position = mAdapter.getLastSeletPosition();
                    listener.onFinish(data);
                }
                dismiss();
            }
        });

        mAdapter = new ArtTypeAdapter(context,true, R.layout.item_sel_location);

        gv_data.setAdapter(mAdapter);
        gv_data.setOnItemClickListener(this);

    }

    private void loadDatas() {
        HttpUitls.onGet("art_category",new OnResponeListener<ArtTypeBean>() {
            @Override
            public void onSuccess(ArtTypeBean bean) {
                Logger.d("art_category==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0) {
                    mDatas = bean.data.list;
                    mAdapter.setDatas(mDatas);
                }
            }

            @Override
            public void onFailed(String error) {
                Logger.d("art_category==>>error = ", error);
            }
        });
    }

    public void dismiss(){
        super.dismiss();
    }

    public void showAsDropDown(View root){
        if(Build.VERSION.SDK_INT >= 24){
            Rect visibleFrame = new Rect();
            root.getGlobalVisibleRect(visibleFrame);
            int height = root.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            setHeight(height);
        }
        super.showAsDropDown(root);
        // 每次打开恢复记录
        if(position != -1){
            mAdapter.changeSel(position);
        }else{
            mAdapter.cleanAllState();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        mAdapter.changeSel(position);
    }

    public void setOnFinishListener(OnFinishListener listener){
        this.listener = listener;
    }

    public interface OnFinishListener{
        void onFinish(String data);
    }


}
